package com.yxw.live_vod_boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_boot.dto.*;
import com.yxw.live_vod_boot.entity.YxOnDemandVideoEntity;
import com.yxw.live_vod_boot.mapper.ShopMapper;
import com.yxw.live_vod_boot.service.SysNoticeService;
import com.yxw.live_vod_boot.utils.UserUtil;
import com.yxw.live_vod_facade.dto.PageOnDemandCommentDTO;
import com.yxw.live_vod_boot.entity.YxOnDemandCommentEntity;
import com.yxw.live_vod_boot.mapper.OnDemandCommentMapper;
import com.yxw.live_vod_boot.mapper.OnDemandVideoMapper;
import com.yxw.live_vod_boot.service.OnDemandCommentService;
import com.yxw.live_vod_boot.utils.PageUtils;
import com.yxw.live_vod_facade.vo.OnDemandCommentVO;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 点播评论管理
 * @author liuxin
 * @date 2023.11.15
 */
@Slf4j
@Service
public class OnDemandCommentServiceImpl extends ServiceImpl<OnDemandCommentMapper, YxOnDemandCommentEntity> implements OnDemandCommentService {

    @Autowired
    private OnDemandCommentMapper onDemandCommentMapper;

    @Autowired
    private SysNoticeService sysNoticeService;

    @Autowired
    private OnDemandVideoMapper onDemandVideoMapper;

    @Autowired
    private UserUtil  userUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OnDemandCommentVO addComment(OnDemandCommentDTO dto) {
        Long userId = userUtil.getLoginUserId();
        if(null==userId){
            throw new BaseException("用户未登录,不能评论");
        }
        YxOnDemandCommentEntity entity = new YxOnDemandCommentEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setCommentTime(new Date());
        entity.setCommentType(CommonConstant.ONE);
        entity.setTopFlag(CommonConstant.ZERO);
        entity.setUserId(userId);
        entity.setUserName(userUtil.getLoginName());
        baseMapper.insert(entity);
        //发送互动消息
        YxOnDemandVideoEntity videoEntity = onDemandVideoMapper.selectById(dto.getVideoId());
        if(null!=videoEntity){
            SendNormalMsgDto msgDto = new SendNormalMsgDto();
            msgDto.setUserName(videoEntity.getUserName());
            msgDto.setUserId(videoEntity.getUserId());
            msgDto.setShopId(videoEntity.getShopId());
            msgDto.setShopName(videoEntity.getShopName());
            msgDto.setType(2);
            sysNoticeService.sendNormalNotice(msgDto);
        }
        OnDemandCommentVO vo = new OnDemandCommentVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OnDemandCommentVO addReplyComment(OnDemandReplyCommentDTO dto) {
        Long userId = userUtil.getLoginUserId();
        if(null==userId){
            throw new BaseException("用户未登录,不能回复");
        }
        YxOnDemandCommentEntity entity = new YxOnDemandCommentEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setCommentTime(new Date());
        entity.setReplyCommentId(dto.getReplyCommentId());
        YxOnDemandCommentEntity commentEntity= baseMapper.selectById(dto.getReplyCommentId());
        entity.setCommentType(CommonConstant.TWO);
        entity.setTopFlag(CommonConstant.ZERO);
        entity.setUserId(userId);
        entity.setUserName(userUtil.getLoginName());
        entity.setVideoId(commentEntity.getVideoId());
        baseMapper.insert(entity);
        //发送互动消息
        SendNormalMsgDto msgDto = new SendNormalMsgDto();
        msgDto.setUserName(commentEntity.getUserName());
        msgDto.setUserId(commentEntity.getUserId());
        msgDto.setType(2);
        //查询回复的是不是当前视频店主，是就给店铺发送消息
        LambdaUpdateWrapper<YxOnDemandVideoEntity> videoWapper=new LambdaUpdateWrapper();
        videoWapper.eq(YxOnDemandVideoEntity::getVideoId,commentEntity.getVideoId());
        videoWapper.eq(YxOnDemandVideoEntity::getUserId,commentEntity.getUserId());
        YxOnDemandVideoEntity videoEntity = onDemandVideoMapper.selectOne(videoWapper);
        if(null!=videoEntity){
            msgDto.setShopId(videoEntity.getShopId());
            msgDto.setShopName(videoEntity.getShopName());
        }
        sysNoticeService.sendNormalNotice(msgDto);
        OnDemandCommentVO vo = new OnDemandCommentVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    /**
     * 查询与我相关的直播评论或者回复
     * @param dto
     * @return
     */
    @Override
    public PageVO<OnDemandCommentVO> pageRelatedComments(PageOnDemandCommentDTO dto) {
        Page<OnDemandCommentVO> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<OnDemandCommentVO> volist=onDemandCommentMapper.selectRelatedComments(page,dto);
        return PageUtils.get(page,volist);
    }

    /**
     * 查询视频下面的评论和回复
     * @param dto
     * @return
     */
    @Override
    public PageVO<OnDemandCommentVO> pageVideoComments(PageOnDemandCommentDTO dto) {
        Page<OnDemandCommentVO> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<OnDemandCommentVO> volist=onDemandCommentMapper.selectVideoComments(page,dto);
        return PageUtils.get(page,volist);
    }

    /**
     * 评论/取消置顶
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void topComments(TopOnDemandCommentDto dto) {
        LambdaUpdateWrapper<YxOnDemandCommentEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(true,YxOnDemandCommentEntity::getTopFlag,dto.getTopFlag())
                .eq(true,YxOnDemandCommentEntity::getCommentId,dto.getCommentId());
        baseMapper.update(null,updateWrapper);
    }

    /**
     * 删除评论(评论删了连带回复一起删除)
     * @param commentId
     */
    @Override
    public void deleteCommentById(Long commentId) {
        //删除评论
        onDemandCommentMapper.deleteCommentById(commentId);
    }
}